
const { User, validateUser } = require('../../model/user');
const bcrypt = require('bcrypt');
module.exports = async (req, res, next) => {
    
    try {
        await validateUser(req.body);
    } catch (err) {
        return next(JSON.stringify({ path: '/admin/user-edit', message: err.message }));
    }
    
    let user = await User.findOne({ email: req.body.email });
    if (user) {
        return next(JSON.stringify({ path: '/admin/user-edit', message: '邮箱地址已被占用' }));
    }
    const salt = await bcrypt.genSalt(10);
    const pass = await bcrypt.hash(req.body.password, salt);
    req.body.password = pass;
    await User.create(req.body);
    res.redirect('/admin/user');
}